#import "SEGPageControl.h"

#if __has_include(<SEGPlatform/SEGPlatformManager.h>)
#import <SEGPlatform/SEGPlatformManager.h>
#else
#import "SEGPlatformManager.h"
#endif

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#import <Masonry/Masonry.h>

static inline CGFloat pageControlHeight(){
    return 3;
}

static inline CGFloat pageControlWidth(){
    return 13;
}

static inline CGFloat pageControlMargin(){
    return 2;
}

@implementation SEGPageControlModel
-(instancetype)init{
    if (self == [super init]) {
        _numberOfPages = 0;
        _currentPage = 0;
    }
    return self;
}
@end

@implementation SEGPageControlLayout
- (instancetype)initWithData:(SEGPageControlModel *)data{
    if (self == [super init]) {
        _data = data;
    }
    [self segPageControlLayout];
    return self;
}

-(void)segPageControlLayout{
    //高度固定
    
    _totalHeight = [SEGPageControlLayout getPageHeightWithType:self.data.type];
    
    //高度不固定
    //_totalHeight = 0;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    CGFloat pageWidth = [SEGPageControlLayout getPageWidthWithType:self.data.type];
    NSInteger pagePointCount = _data.numberOfPages * pageWidth;
    NSInteger pageMarginCount = (_data.numberOfPages - 1) * [SEGPageControlLayout getPageMarginWithType:self.data.type];
    _totalWidth = pagePointCount + pageMarginCount;
}

+ (CGFloat)getPageWidthWithType:(SEGPageControlLayoutType)type {
    if (type == SEGPageControlLayoutType_House) {
        return 6;
    }
    return pageControlWidth();
}

+ (CGFloat)getPageHeightWithType:(SEGPageControlLayoutType)type {
    if (type == SEGPageControlLayoutType_House) {
        return 6;
    }
    return pageControlHeight();
}

+ (CGFloat)getPageMarginWithType:(SEGPageControlLayoutType)type {
    if (type == SEGPageControlLayoutType_House) {
        return 5;
    }
    return pageControlMargin();
}

@end

@implementation SEGPageControl
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = pageControlHeight();
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.backScrollView];
    
    return self;
}

-(void)setWithLayout:(SEGPageControlLayout *)layout{
    _layout = layout;
    self.height = layout.totalHeight;
    self.width = layout.totalWidth;
    self.backScrollView.frame = CGRectMake(0, 0, self.width, self.height);
    
    if (self.height <= 0) return;
    
    [self setupUI];
}

/**
 * 设置当前展示的页面坐标
 * @author Sam
 */
-(void)setCurrentPage:(NSInteger)currentIndex{
    for (UIImageView *object in self.backScrollView.subviews) {
        if (currentIndex == object.tag) {
            object.backgroundColor = kPageControlColorWhiteA;
            self.layout.data.currentPage = currentIndex;
        }else{
            object.backgroundColor = kPageControlColorWhiteH;
        }
    }
}

//界面布局
-(void)setupUI{
    [self.backScrollView removeAllSubviews];
    CGFloat pageWidth = [SEGPageControlLayout getPageWidthWithType:self.layout.data.type];
    CGFloat pageHegiht = [SEGPageControlLayout getPageHeightWithType:self.layout.data.type];
    CGFloat pageMargin = [SEGPageControlLayout getPageMarginWithType:self.layout.data.type];
    for (NSInteger i = 0; i < self.layout.data.numberOfPages; i++) {
        CGFloat X = (pageWidth + pageMargin) * i;
        CGRect rect = CGRectMake(X, 0,pageWidth,pageHegiht);
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect];
        imageView.layer.cornerRadius = 1.3;
        if(self.layout.data.type == SEGPageControlLayoutType_House) {
            imageView.layer.cornerRadius = pageWidth * 0.5;
        }
        imageView.layer.masksToBounds = YES;
        imageView.backgroundColor = kPageControlColorWhiteH;
        imageView.tag = i;
        [self.backScrollView addSubview:imageView];
        
        if (i == self.layout.data.currentPage) {
            imageView.backgroundColor = kPageControlColorWhiteA;
        }
    }
}

#pragma mark - getter
-(UIScrollView *)backScrollView{
    if (!_backScrollView) {
        _backScrollView = [[UIScrollView alloc]init];
        _backScrollView.frame = self.frame;
        _backScrollView.backgroundColor = [UIColor clearColor];
        _backScrollView.contentSize = CGSizeMake(0, 0);
    }
    return _backScrollView;
}


@end

